package com.yanqu.road.dao.impl.activity.gashapon;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.gashapon.data.GashaponNotice;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-10-21
 * @Description : 幸运扭蛋-公告 Dao
 */

public class GashaponNoticeDaoImpl extends TempDao {
    public static String getGashaponNoticeDataSql = "select * from `t_c_gashapon_notice` where `activityId`=? order by `noticeTime` desc limit 100";

    public List<GashaponNotice> getGashaponNoticeData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<GashaponNotice> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGashaponNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parseData(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertGashaponDrawNoticeDataSql = "insert into `t_c_gashapon_notice`(activityId,userId,serverId,nickName,rewardType,reward,noticeTime) values (?,?,?,?,?,?,?)";

    public boolean addNoticeList(List<GashaponNotice> noticeDataList) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (GashaponNotice noticeData : noticeDataList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;

            para.put(i++, new DbParameter(Types.INTEGER, noticeData.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, noticeData.getUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, noticeData.getServerId()));
            para.put(i++, new DbParameter(Types.VARCHAR, noticeData.getNickName()));
            para.put(i++, new DbParameter(Types.INTEGER, noticeData.getRewardType()));
            para.put(i++, new DbParameter(Types.VARCHAR, noticeData.getReward()));
            para.put(i++, new DbParameter(Types.BIGINT, noticeData.getNoticeTime()));

            mapList.add(para);
        }

        if (getDb().executeBatchQuery(insertGashaponDrawNoticeDataSql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    protected GashaponNotice parseData(ResultSet rs) throws SQLException {
        GashaponNotice noticeData = new GashaponNotice();
        noticeData.setActivityId(rs.getInt("activityId"));
        noticeData.setUserId(rs.getLong("userId"));
        noticeData.setServerId(rs.getLong("serverId"));
        noticeData.setNickName(rs.getString("nickName"));
        noticeData.setRewardType(rs.getInt("rewardType"));
        noticeData.setReward(rs.getString("reward"));
        noticeData.setNoticeTime(rs.getLong("noticeTime"));

        return noticeData;
    }
}
